package com.wjk.listener;

import lombok.SneakyThrows;
import org.apache.ibatis.io.Resources;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author: RobertWei
 * time: 2021/8/12    15:58
 * @TODO:
 */

@WebListener
public class PropertiesLoader implements ServletContextListener {

    @SneakyThrows
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {

        InputStream stream = Resources.getResourceAsStream("url/anonymity.properties");
        InputStream stream1 = Resources.getResourceAsStream("url/authentication.properties");

        //程序启动会自动触发
        Properties anonymity = new Properties();
        Properties authentication = new Properties();
        anonymity.load(stream);
        authentication.load(stream1);



        //放入共享域对象
        ServletContext servletContext = servletContextEvent.getServletContext();
        servletContext.setAttribute("anonymity",anonymity);
        servletContext.setAttribute("authentication",authentication);


    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
